var layerIndex;
var httpUrl='http://www.main.com:8082/api/';
$(function () {
   deviceReady();
})
function gotoDetail(url,force) {
    window.location.href = url;
}
function beginAsyncAjax(url, requestData, type, succeed, failed,noloading) {
    showLoading();
    ajax(type, url, requestData, succeed, failed);
}
/*ajax封装方法*/
function ajax(method, url, datas, cb, errf) {
    if(url.indexOf('ttps://') > 0 || url.indexOf('ttp://') > 0){}else{
        url = httpUrl+url;
    }
    var token = getStorage('token');
    if(token == null || token == 'null'){
        token = '';
    }

    $.ajax({
        url : url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'token':token
            // 'Access-Control-Allow-Origin':'*'
        },
               // contentType: "application/x-www-form-urlencoded",
        data : datas,
        type : method,
        //        async:true,
        dataType : "json",
        success : function(data) {
            hideLoading();
            cb(data);
        },
        error : function(err) {
            hideLoading();
            errf(err);
        }
    });
}
function showLoading() {
    //正在加载
    layerIndex = layer.load(1, {
        shade: [0.1,'#fff']
    });
}
function hideLoading() {
    layer.close(layerIndex);//关闭加载
}
//公共提示框
function msg_alert(message,url){

    if(url){
        layer.msg(message,{time:1000},function(){
            window.location.href=url;
        });
    }else{
        layer.msg(message,{time:1500});
    }
}
// 获得页面传递的参数(GET)
function getArgumentsFromPageURL() {
    //构造参数对象并初始化
    var argumentsObj = new Object();
    var strURL = location.href.toString();
    var strArgumentsPartInURL = null;

    if ((strURL != undefined) && (strURL != null)) {
        var indexOfSeperator = strURL.indexOf("?");
        if (indexOfSeperator > 0) {
            //截取“?”后面的参数串
            strArgumentsPartInURL = strURL.substr(indexOfSeperator + 1);
            var indexKao = strArgumentsPartInURL.indexOf("?");
            if (indexKao > 0) {
                //截取“?”后面的参数串
                strArgumentsPartInURL = strArgumentsPartInURL.substr(indexKao + 1);
            }
        }
    }

    if ((strArgumentsPartInURL != undefined) && (strArgumentsPartInURL != null) && (strArgumentsPartInURL.length > 0)) {
        //将各参数分离形成参数数组
        var arrKeyValueOfArguments = strArgumentsPartInURL.split("&");
        for (var start = 0; start < arrKeyValueOfArguments.length; start++) {
            var strKeyValueOfArgument = arrKeyValueOfArguments[start];
            if ((strKeyValueOfArgument != undefined) && (strKeyValueOfArgument != null) && (strKeyValueOfArgument.length > 0)) {
                var indexOfKeyValueSeperator = strKeyValueOfArgument.indexOf("=");
                if (indexOfKeyValueSeperator > 0) {
                    //取得参数名称
                    var keyWithURLEncode = strKeyValueOfArgument.substring(0, indexOfKeyValueSeperator);
                    var key = decodeURI(keyWithURLEncode);
                    //取得参数值
                    var valueWithURLEncode = strKeyValueOfArgument.substr(indexOfKeyValueSeperator + 1);
                    var value = decodeURI(valueWithURLEncode);
                    //定义对象属性并初始化
                    argumentsObj[key] = value;
                }
            }
        }
    }
    return argumentsObj;
}
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}
/**
 * 设置缓存
 * @param key
 * @param value
 */
function setStorage(key,value) {
    localStorage.setItem(key,value);
}

/**
 * 获取缓存
 * @param key
 * @returns {string | null}
 */
function getStorage(key) {
    return localStorage.getItem(key);
}

/**
 * 删除缓存
 * @param key
 */
function delStorage(key) {
    localStorage.removeItem(key);
}
